<template>
  <div class="home-container">
    <el-container>
      <!-- 头部区域 -->
      <el-header>
        <h2 class="header-title">购物管理系统</h2>
      </el-header>

      <el-container>
        <!-- 侧边栏菜单 -->
        <el-aside width="200px">
          <el-menu
            :default-active="activeMenu"
            class="el-menu-vertical-demo"
            @select="handleMenuSelect"
          >
            <el-menu-item index="/adminhomeproduct">
              <i class="el-icon-goods"></i>
              <span>商品管理</span>
            </el-menu-item>
            <el-menu-item index="/adminhomeorder">
              <i class="el-icon-s-order"></i>
              <span>订单管理</span>
            </el-menu-item>
            <el-menu-item index="/adminhomeuser">
              <i class="el-icon-user"></i>
              <span>用户管理</span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        <!-- 主内容区域 -->
        <el-main>
          <el-button
            type="primary"
            @click="GoBack"
            style="float: left; margin-bottom: 10px"
          >
            返回
          </el-button>
          <!-- 订单表格 -->

          <el-table
            ref="multipleTable"
            :data="userOrderLogData.data"
            tooltip-effect="dark"
            style="width: 100%; font-size: 20px"
            @selection-change="handleSelectionChange"
          >
            <el-table-column width="40"></el-table-column>
            <el-table-column label="商品图片" width="200">
              <template #default="scope">
                <img
                  :src="scope.row.image"
                  alt="SmallImage"
                  class="resized-image"
                />
              </template>
            </el-table-column>
            <el-table-column
              prop="name"
              label="商品名称"
              width="350"
              show-overflow-tooltip
            ></el-table-column>
            <el-table-column
              prop="price"
              label="单价"
              width="140"
            ></el-table-column>
            <el-table-column
              prop="quantity"
              label="数量"
              width="100"
            ></el-table-column>
            <el-table-column label="小计" width="180">
              <template #default="scope">
                <div class="TotalPrice">
                  {{ scope.row.price * scope.row.quantity }}元
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="userId"
              label="用户id"
              width="100"
            ></el-table-column>
            <el-table-column label="购买时间" width="200">
              <template #default="scope">
                {{ formatDate(scope.row.purchaseTime) }}
              </template>
            </el-table-column>
          </el-table>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeMenu: '/adminhomeuser', // 默认选中的菜单项
      userOrderLogData: {}, // 用户浏览日志数据
    }
  },
  mounted() {
    this.getuserOrderLogData()
  },
  methods: {
    GoBack() {
      this.$router.push('/adminhomeuser')
    },

    // 格式化时间为可读的形式
    formatDate(dateStr) {
      const date = new Date(dateStr) // 将字符串转换为 Date 对象
      return date.toLocaleString() // 使用浏览器的默认格式输出时间（日期和时间）
    },
    handleMenuSelect(index) {
      console.log(`选中菜单项: ${index}`)
      this.activeMenu = index
      // 跳转到对应的路由路径
      this.$router.push(index).catch((err) => {
        // 捕获错误，避免重复点击报错
        if (err.name !== 'NavigationDuplicated') {
          console.error(err)
        }
      })
    },
    //获取用户数据
    getuserOrderLogData() {
      const token = localStorage.getItem('jwtToken') // 获取存储的 token
      const id = this.$route.params.id //获取当前路由中动态路由的参数
      this.$http
        .get('/api/adminhome/order/' + id, {
          headers: {
            Authorization: `Bearer ${token}`, // 使用 Bearer 方案
          },
        })
        .then((res) => {
          this.userOrderLogData = res.data
          console.log(this.userOrderLogData.data)
        })
        .catch((err) => {
          console.log(err)
        })
    },
  },
}
</script>

<style scoped>
.header-title {
  padding-left: 200px;
}
.resized-image {
  max-width: 80px;
  max-height: 80px;
  object-fit: contain;
}
</style>